package top.lyc829.zzl_android.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.xuexiang.xpage.base.XPageActivity;

public abstract class BaseActivity extends XPageActivity {

    public Context mContext;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;
    }

    /**
     * 显示Toast
     * @param msg 待显示的字符串
     */
    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示Toast的同步版本，在子线程中使用
     * @param msg 待显示的字符串
     */
    public void showToastSync(String msg) {
        Looper.prepare();
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }

    /**
     * 跳转到下一个Activity
     * @param cls 下一个Activity的类名
     */
    public void navigateTo(Class cls) {
        Intent in = new Intent(mContext, cls);
        startActivity(in);
    }

    /**
     * 跳转到下一个Activity,带标志
     * @param cls 下一个Activity的类名
     */
    public void navigateToWithFlag(Class cls, int flags) {
        Intent in = new Intent(mContext, cls);
        in.setFlags(flags);
        startActivity(in);
    }

    /**
     * 向SharedPreferences中插入键值对
     * @param key 键
     * @param val 值
     */
    protected void insertToSp(String key, String val) {
        SharedPreferences sp = getSharedPreferences("sp_zzl", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, val);
        editor.apply();
    }

    /**
     * 在SharedPreferences中根据键读取值
     * @param key 键
     * @return    对应的值，没有则返回空字符串
     */
    protected String getValueFromSp(String key) {
        SharedPreferences sp = getSharedPreferences("sp_zzl", MODE_PRIVATE);
        return sp.getString(key, "");
    }



}
